package com.server.code.controller;

import com.reason.common.pojo.Result;
import com.reason.common.pojo.StatusCode;
import com.reason.common.utils.SMSUtils;
import com.reason.common.utils.ValidateCodeUtils;
import com.reason.common.enums.RedisMessageStatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author tang wei
 * @version 1.0
 * @date 2020/7/1 12:38
 */
@RestController
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     *用户发送注册验证码
     * @param phone 手机号码
     * @return 验证码是否发送成功
     */
    @GetMapping("/getRegisterCode/{telephone}")
    public Result getCode(@PathVariable("telephone") Long phone ){
        //定义验证的长度
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        // 限制发送频率
        String duplicate = stringRedisTemplate.opsForValue().get(phone + RedisMessageStatusCode.SENDTYPE_DUPLICATE);
        if (duplicate != null) {
            return new Result(false,StatusCode.ERROR,"验证码发送过于频繁");
        }
        try{
            //发送验证码
            SMSUtils.sendShortMessage(SMSUtils.REGISTER_CODE,String.valueOf(phone),validateCode.toString());
            //发送验证码成功,存入redis中,key为手机号码+001
            stringRedisTemplate.opsForValue().set(phone + RedisMessageStatusCode.SENDTYPE_REGISTER,validateCode.toString(),60*5, TimeUnit.SECONDS);
            stringRedisTemplate.opsForValue().set(phone + RedisMessageStatusCode.SENDTYPE_DUPLICATE,validateCode.toString(),60, TimeUnit.SECONDS);
            //发送验证码成功,返回结果
            return new Result(true, StatusCode.OK,"验证码发送成功");
        }catch (Exception e){
            //发送验证码失败
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"验证码发送失败");
        }
    }

    /**
     *用户发送登录验证码
     * @param phone 手机号码
     * @return 验证码是否发送成功
     */
    @GetMapping("/getLoginCode/{telephone}")
    public Result getLoginCode(@PathVariable("telephone") Long phone ){
        //定义验证的长度
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        // 限制发送频率
        String duplicate = stringRedisTemplate.opsForValue().get(phone + RedisMessageStatusCode.SENDTYPE_DUPLICATE);
        if (duplicate != null) {
            return new Result(false,StatusCode.ERROR,"验证码发送过于频繁");
        }
        try{
            //发送验证码
            SMSUtils.sendShortMessage(SMSUtils.LOGIN_CODE,String.valueOf(phone),validateCode.toString());
            //发送验证码成功,存入redis中，key为电话号码+002
            stringRedisTemplate.opsForValue().set(String.valueOf(phone)+ RedisMessageStatusCode.SENDTYPE_LOGIN,validateCode.toString(),60*5, TimeUnit.SECONDS);
            stringRedisTemplate.opsForValue().set(phone + RedisMessageStatusCode.SENDTYPE_DUPLICATE,validateCode.toString(),60, TimeUnit.SECONDS);
            //发送验证码成功,返回结果
            return new Result(true, StatusCode.OK,"验证码发送成功");
        }catch (Exception e){
            //发送验证码失败
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"验证码发送失败");
        }
    }

    /**
     *用户发送修改密码验证码
     * @param phone 手机号码
     * @return 验证码是否发送成功
     */
    @GetMapping("/getUpdatePasswordCode/{telephone}")
    public Result getUpdatePassword(@PathVariable("telephone") Long phone ){
        //定义验证的长度
        Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
        // 限制发送频率
        String duplicate = stringRedisTemplate.opsForValue().get(phone + RedisMessageStatusCode.SENDTYPE_DUPLICATE);
        if (duplicate != null) {
            return new Result(false,StatusCode.ERROR,"验证码发送过于频繁");
        }
        try{
            //发送验证码
            SMSUtils.sendShortMessage(SMSUtils.UPDATE_PASSWORD_CODE,String.valueOf(phone),validateCode.toString());
            //发送验证码成功,存入redis中，key为电话号码+003
            stringRedisTemplate.opsForValue().set(String.valueOf(phone)+ RedisMessageStatusCode.SENDTYPE_GETPWD,validateCode.toString(),60*5, TimeUnit.SECONDS);
            stringRedisTemplate.opsForValue().set(phone + RedisMessageStatusCode.SENDTYPE_DUPLICATE,validateCode.toString(),60, TimeUnit.SECONDS);
            //发送验证码成功,返回结果
            return new Result(true, StatusCode.OK,"验证码发送成功");
        }catch (Exception e){
            //发送验证码失败
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"验证码发送失败");
        }
    }

}
